<?php

namespace Api\Controller;

use Think\Controller;

class ApiController extends Controller {

    private $error_token;
    protected $classnum;

    //warntype:1.加入班级，2.发布消息，3.被踢出班级，4.回复消息icon
    function __construct() {
        parent::__construct();
        $this->classnum = 5;
        $this->error_token = L('error_token');
        header("Access-Control-Allow-Origin:*");
        $this->is_version(I('request.version'));
    }

    /**
     * 版本判断
     * @param type $version
     */
    private function is_version($version) {
        if ($version < C('VERSION')) {
            
        }
    }

    /**
     * 错误返回
     * return error;
     */
    protected function error($strError, $token = 1) {
        $arrError = array('token' => $token, 'strError' => $strError);
        $Error = array(
            'status' => 'error',
            'errorinfo' => $arrError
        );
        echo json_encode($Error);
        die;
    }

    /**
     * 正确返回
     * return success
     */
    protected function success($func, $arrInfo) {
        $arrRight = array(
            'status' => 'ok',
            'data' => array(
                'function' => $func,
                'Info' => $arrInfo
            ),
        );
        echo json_encode($arrRight);
        die;
    }

    /**
     * verifytoken
     * @param type $token
     * @param type $deviceid
     * @return type
     */
    protected function verify_token($token, $deviceid) {
        $where = array('token' => $token);
        $userlog = M('Signinlog')->where($where)->order('id desc')->limit(1)->find();
        if (!$userlog['user_id'] || empty($userlog['user_id'])) {
            $this->error($this->error_token['tokenmatch'], 0);
        }
        if ($userlog['deviceid'] != $deviceid) {
            $this->error($this->error_token['deviceidmatch'], 0);
        }
        $updata = array(
            'signtime' => date('Y-m-d H:i:s')
        );
        $where = array('id' => $userlog['id']);
        M('Signinlog')->where($where)->save($updata);
        return $userlog;
    }

    protected function is_teacher($user_id) {
        $where = array('id' => $user_id);
        $user = M('Users')->where($where)->limit(1)->find();
        return $user;
    }

    public function __call($method, $args) {
        header("HTTP/1.0 404 Not Found");
    }

    protected function jpushapi($uids, $pushText, $m_type = 'http', $m_txt = '', $m_time = '600') {
        $mJpush = new \Org\Util\Jpush();
        switch ($uids) {
            case $uids=='all':
                $aReceive = 'all';
                break;
            case is_array($uids):
                $aReceive = array('alias' => $uids);
                break;
            default :
                $aReceive = array('alias' => array($uids));
                break;
        }
        $content = $pushText;
        //调用推送,并处理
        $result = $mJpush->push($aReceive, $content, $m_type, $m_txt, $m_time);
        if ($result) {
            $res_arr = json_decode($result, true);
            if (isset($res_arr['error'])) {                       //如果返回了error则证明失败
                //echo $res_arr['error']['message'].'<br/>';          //错误信息
                //echo $res_arr['error']['code'];             //错误码
               // \Think\Log::record($res_arr['error']['message'] . '|' . $res_arr['error']['code']);
                return false;
            } else {

                return true;
            }
        } else {      //接口调用失败或无响应
            //echo '接口调用失败或无响应';
            return false;
        }
    }
    function demo(){
        $cid=I('request.cid');
        $uid=I('request.uid');
        var_dump(D('Member')->getuList($uid,$cid,0,30));
    }

}
